<?php

namespace App\Http\Controllers\H5;

use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use App\Common\Service\ArticleService;
use App\Model\Temp;
use Carbon\Carbon;
use Goutte\Client;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use \Log;

class ArticleController extends Controller
{
    public $articleService;

    /**
     * H5Controller constructor.
     * @param $articleService
     */
    public function __construct(ArticleService $articleService)
    {
        $this->articleService = $articleService;
    }

    /**
     * 分类接口
     */
    public function categories(Request $request)
    {
        $p_cate_name = $request->get('p_cate_name');
        if (!$p_cate_name) {

            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
        $categories = $this->articleService->getCategories($p_cate_name);

        Log::info("获取文章分类名称：" . json_encode($categories));
        if (!$categories) {
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }

        $data = $categories;

        return ApiResponse($data);
    }

    /**
     * 首页文章列表
     * @param Request $request
     * @throws AixinException
     */
    public function homeList(Request $request)
    {
        $cateIdStr = $request->get('cate_id');
        if (strpos($cateIdStr, ',') === false) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
        $cateIdArr = explode(',', $cateIdStr);
        $data = [];
        foreach ($cateIdArr as $cateId) {
            $articles = $this->articleService->getH5HomeList($cateId);
            $data[] = $articles;
        }
        if (empty($data)) {
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }
        Log::info('首页文章列表' . json_encode($data));
        return ApiResponse($data);
    }

    /*
     * 文章列表
     */
    public function articleList(Request $request)
    {
        $cateId = $request->get('cate_id');
        if (empty($cateId)) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
        $articles = $this->articleService->getH5List($cateId);
        $articles['cate_id'] = (int)$cateId;
        // dd($articles);
        if (empty($articles)) {
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }
        Log::info('新闻中心文章列表数据' . json_encode($articles));
        return ApiResponse($articles);

    }

    /**
     * 文章详情
     */
    public function detail(Request $request)
    {
        $id = $request->get('id');
        if (empty($id)) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }

        $detail = $this->articleService->getH5Detail($id);
        if (empty($detail)) {
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }
        Log::info('文章详情内容:' . json_encode($detail));
        return ApiResponse($detail);
    }

    /**
     * 手机站相关新闻
     * @param Request $request
     * @return array
     * @throws AixinException
     */
    public function aboutNews(Request $request)
    {
        $cateId = $request->get('cate_id');
        if (empty($cateId)) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
        $aboutNews = $this->articleService->getH5AboutNews($cateId);
        if (empty($aboutNews)) {
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }
        Log::info('手机站相关新闻:' . json_encode($aboutNews));

        return ApiResponse($aboutNews);
    }

    /**
     * 手机站股价
     */
    public function stock()
    {
        if (\cache()->has('stock')){
            $data = \cache('stock');
        }else{
            $re = $this->_getContents("https://hk.finance.yahoo.com/quote/AIXN?p=AIXN");
            preg_match('/data-reactid="35"><\!-- react-text: 36 -->(\d+\.\d+)/ism', $re, $res1);
            preg_match('/data-reactid="37"><\!-- react-text: 38 -->.*?([+-]{0,1}\d+\.\d+%)/ism', $re, $res2);
            unset($res1[0],$res2[0]);
            $arr = array_merge($res1,$res2);
            $arr[1] = substr($arr[1],0,strlen($arr[1])-1);
            if (!isset($arr[1]) || empty($arr[1])){
                $arr[1] = 1;
            }
            if ($arr[1] >= 0){
                $status = 1;
            }else{
                $status = 0;
            }
            if (empty($arr)){
                throw new AixinException(AixinExceptionCode::RESULT_NULL);
            }
            $data = ['stock'=>$arr[0],'status'=>$status];
            \cache()->add('stock',$data,10);
        }
        return ApiResponse($data);
    }

    private function _getContents($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }


}
